<?php

namespace App\Console\Commands\Manual;

use App\Models\Common\Wechat;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class WechatMenu extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:wechat_menu';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '刷新公众号菜单';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $app = Wechat::officialAccount();

        // 刷新删除微信菜单
        try{
            $app->menu->delete();
            $this->info("menu删除成功,正在上传..");

            $url = env("APP_LAIFEI_URL");
            $buttons = [
                [
                    "type" => "view",
                    "name" => "任务大厅",
                    "url"  => $url.'/talent',
                ],
                [
                    "type" => "view",
                    "name" => "已接任务",
                    "url"  => $url.'/talent/mine',
                ],
                [
                    "name"       => "个人中心",
                    "sub_button" => [
                        [
                            "type" => "view",
                            "name" => "我的账号",
                            "url"  => $url.'/talent/list',
                        ],
                        [
                            "name" => "在线客服",
                            "type" => "click",
                            "key"  => "1",
                        ],
                        [
                            "type" => "view",
                            "name" => "霸王餐",
                            "url"  => $url.'/lottery',
                        ],
                        [
                            "type" => "view",
                            "name" => "商户入口",
                            "url"  => $url
                        ],
                    ],
                ],
            ];
//            $buttons = [
//                [
//                    "name"       => "店主",
//                    "sub_button" => [
//                        [
//                            "type" => "view",
//                            "name" => "店主首页",
//                            "url"  => $url
//                        ],
//                        [
//                            "type" => "view",
//                            "name" => "活动进度",
//                            "url"  => $url.'/history',
//                        ],
//                        [
//                            "type" => "view",
//                            "name" => "订单管理",
//                            "url"  => $url.'/mine',
//                        ],
//                        [
//                            "type" => "view",
//                            "name" => "我的店铺",
//                            "url"  => $url.'/shop',
//                        ],
//                    ],
//                ],[
//                    "name"       => "🔥在线咨询",
//                    "type" => "click",
//                    "key"  => "1",
//                ],[
//                    "name"       => "达人",
//                    "sub_button" => [
//                        [
//                            "type" => "view",
//                            "name" => "霸王餐",
//                            "url"  => $url.'/lottery',
//                        ],
//                        [
//                            "type" => "view",
//                            "name" => "写点评",
//                            "url"  => $url.'/talent/history',
//                        ],
//                        [
//                            "type" => "view",
//                            "name" => "我的订单",
//                            "url"  => $url.'/lottery/mine',
//                        ],
//                        [
//                            "type" => "view",
//                            "name" => "我的账号",
//                            "url"  => $url.'/talent/list',
//                        ],
//                    ],
//                ],
//            ];
            $app->menu->create($buttons);
            $this->info("公众号menu更新成功");
            return true;
        }catch (\Exception $e){
            Log::error(WechatMenu::class.':删除微信菜单失败', ['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]);
            $this->error(WechatMenu::class.':删除微信菜单失败');
            $this->error(json_encode( ['msg' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()]));
            return false;
        }

    }
}
